let app = getApp()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        //颜色按钮选中的颜色
        btnCheckedColor: "#FCB300",
        //{"name":"白色","btnColor":"","checked":false}
        //所有的颜色数据
        colorList: [],
        //选择的颜色list
        checkedColorList: [],
        //长按的数组索引
        longPressIndex: '',
        //action-sheet
        actions: [
            {name: '编辑', color: '#07c160', type: "edit"},
            {name: '删除', color: '#EE0000', type: "del"},
        ],
        actionSheetShow: false,
        //popup
        popupShow: false,
        editPopupShow: false,
        //添加颜色的输入值
        addColorValue: "",
        editColorValue: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let checkedColorList = JSON.parse(options.checkedColorList)
        console.log(checkedColorList)
        if (checkedColorList && checkedColorList.length > 0) {
            this.setData({
                checkedColorList: checkedColorList
            })
        }
        if (app.globalData.setupInfo) {
            this.initColorList()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },
    initColorList: function () {
        let colors = app.globalData.setupInfo.colors
        let checkedColorList = this.data.checkedColorList
        if (colors && colors.length > 0) {
            let colorList = []
            for (let i = 0; i < colors.length; i++) {
                let color = {}
                let name = colors[i]
                color.name = name
                //判断是否为选中状态
                if (checkedColorList.indexOf(name) > -1) {
                    color.btnColor = this.data.btnCheckedColor
                    color.checked = true
                }
                colorList.push(color)
            }
            this.setData({
                colorList: colorList
            })
        }
    },

    colorChecked: function (e) {
        let index = e.currentTarget.id
        let colorList = this.data.colorList
        let color = colorList[index]
        color.btnColor = color.btnColor ? "" : this.data.btnCheckedColor
        color.checked = color.checked ? false : true
        this.setData({
            colorList: colorList
        })
    },
    colorLongPress: function (e) {
        let index = e.currentTarget.id
        this.setData({
            actionSheetShow: true,
            longPressIndex: index
        })
    },
    handleConfirm: function () {
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        //过滤出选中的
        let checkedColorList = this.data.colorList.filter(t => t.checked).map(t => t.name)
        prevPage.setData({
            checkedColors: checkedColorList
        })
        wx.navigateBack({
            delta: 1,
        })
    },
    addColor: function () {
        this.setData({popupShow: true});
    },
    actionSelect: function (e) {
        let colorList = this.data.colorList
        let longPressIndex = this.data.longPressIndex
        let type = e.detail.type
        if (type == 'edit') {
            this.setData({editPopupShow: true});
        } else if (type == 'del') {
            colorList.splice(longPressIndex, 1)
            this.setData({
                colorList: colorList
            })

            if (app.globalData.setupInfo && app.globalData.setupInfo.colors) {
                let colors = app.globalData.setupInfo.colors
                colors.splice(longPressIndex, 1)
                //更新用户设置
                this.handleSetup()
            }
        }
    },
    actionCancel() {
        this.setData({actionSheetShow: false});
    },
    actionClose: function () {
        this.setData({actionSheetShow: false});
    },
    cancelAddColor: function () {
        this.setData({popupShow: false});
    },
    cancelEditColor: function () {
        this.setData({editPopupShow: false});
    },
    confirmAddColor: function () {
        let colors = []
        if (app.globalData.setupInfo && app.globalData.setupInfo.colors) {
            colors = app.globalData.setupInfo.colors
            colors.push(this.data.addColorValue)
        } else {
            colors.push(this.data.addColorValue)
        }
        app.globalData.setupInfo.colors = colors

        let colorList = this.data.colorList
        let colorObj = {}
        //添加的颜色默认选中
        colorObj.name = this.data.addColorValue
        colorObj.btnColor = this.data.btnCheckedColor
        colorObj.checked = true
        colorList.push(colorObj)
        this.setData({
            colorList: colorList,
            addColorValue: ''
        })
        //更新用户设置
        this.handleSetup()
    },
    confirmEditColor: function () {
        let colorList = this.data.colorList
        let longPressIndex = this.data.longPressIndex
        let colorObj = colorList[longPressIndex]
        colorObj.name = this.data.editColorValue
        this.setData({
            colorList: colorList,
            editColorValue: ""
        })

        if (app.globalData.setupInfo && app.globalData.setupInfo.colors) {
            let colors = app.globalData.setupInfo.colors
            colors[longPressIndex] = this.data.editColorValue
            //更新用户设置
            this.handleSetup()
        }
    },
    handleSetup() {
        let _this = this
        let data = {}
        data.colors = app.globalData.setupInfo.colors
        app.formPost('/app/boss/setup/edit', data).then(res => {
            _this.setData({
                spinShow: false
            });
            if (res.code === 0) {
                app.message(res.message, 'success')
                this.setData({popupShow: false});
                this.setData({editPopupShow: false});
            } else {
                app.message(res.message, 'error')
            }
        }).catch(e => {
            _this.setData({
                spinShow: false
            });
            app.message(e, 'error')
        })
    }
})



